<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_user_model extends MY_Model
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 检测登陆状态
	 *
	 * @return boolean
	 */
	public function get_login_status()
	{
		$is_login = FALSE;
		$admin_username = $this->session->userdata('yy_admin_username')?$this->session->userdata('yy_admin_username'):"";
		if (!empty($admin_username)){
			$is_login = TRUE;
		}
		return $is_login;
	}


    
    /**
     * 检查用户是否存在
     * @author  KevinBai
     * @param   string  $name   用户名
     * @param   int     $type   类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_user($username)
    {
        //验证用户名是否存在
        $this->db->where("username",$username);
        $res = $this->db->get('yy_admin_user');
        if($res->num_rows()==0)
        {
            return FALSE;
        }
        return TRUE;
    }  
    /**
     * 检查邮箱是否存在
     * @author  KevinBai
     * @param   string  $name   用户名
     * @param   int     $type   类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_email($email)
    {
        //验证用户名是否存在
        $this->db->where("email",$email);
        $res = $this->db->get('yy_admin_user');
        if($res->num_rows()==0)
        {
            return FALSE;
        }
        return TRUE;
    } 

/**
	 * 重置密码
	 * @param int $uid
	 * @return string|boolean
	 */
	function reset_password($uid,$newpassword) {
		$this->load->helper('md6');
		$uid=intval($uid);
		$newpassword=md6($newpassword);
		$sql="update bp_admin_user set password='{$newpassword}' where id={$uid}";
		return $this->db->query($sql);
	}
   
    
     /**
     * 根据uid获取用户信息
     */
    public function get_userinfo($uid, $login_name)
    {
    	if (!empty($uid))
    	{
    		$this->db->where('id',$uid);
    	}
    	if (!empty($login_name))
    	{
	    	if(stripos($login_name,"@")!==FALSE)
	        	{
	        		$this->db->where('email',$login_name);
	        	}else{
	        		$this->db->where('username',$login_name);
	        	}
    	}
    	$query = $this->db->get('yy_admin_user');
    	return $query->row();
    }
	/**
	 * 检测登陆账号
	 *
	 * @param string $username
	 * @param string $password
	 * @return array
	 */
	public function check_account($username='', $password='')
	{
		$arr_res = array();

		$username = trim(addslashes($username));
		$password = trim(addslashes($password));

		$password = md6($password);

        $where = " ";
        if(stripos($username,"@")!==FALSE)
        {
            //$this->db->where('email', $username);
            $where .= " and user.email = '{$username}'";
        }
        else
        {
           //$this->db->where('username', $username);
            $where .= " and user.username = '{$username}'";
        }

        $where .= " and user.password = '{$password}'";
		//$this->db->where('password', $password);

        $sql  = "SELECT `user`.*,role.`name` role_name,role.power from yy_admin_user as user ,yy_admin_role as role where `user`.roleid=role.id and `user`.isDel=0" .$where;
       $query = $this->db->query($sql);
		//$query = $this->db->get('bp_admin_user');
		if ($query->num_rows() > 0){
			$row = $query->row_array();
			if ($row['status'] != 1){
				$arr_res = array('status'=>1, 'message'=>'用户被禁用');
			}else{
				//获取用户权限
				//将用户信息写入session
                session_start();
                $_SESSION['yy_admin_id'] = $row['id'];
                $_SESSION['yy_admin_username'] = $row['username'];
                $_SESSION['userInfo'] = $row;
				//更新用户登陆日志
				$login_info = array(
						'lastlogin' => time(),
						'lastip'    => $this->input->ip_address()
				);
				$this->db->where('id', $row['id']);
				$this->db->update('yy_admin_user', $login_info);
				$arr_res = array('status'=>0, 'message'=>'','id'=>$row['id']);
			}
		}else{
			$arr_res = array('status'=>2, 'message'=>'用户名或密码错误');
		}
		return $arr_res;
	}
    
    /**
     *
     * 通过用户email获取用户信息
     */
	public function get_userinfo_by_email($email)
	{
		$this->db->where('email',$email);
		$query=$this->db->get('yy_admin_user');
        if($query->num_rows()>0)
        {
            $res=$query->row_array();
		    return $res;
        }
		return null;
	}
    /**
	 * 更新找回密码表
	 * @param String   $uid    用户ID
	 * @param int      $expire 过期时间
	 * @param String   $mkey   用户密钥
	 * @return boolean
	 */
	function update_forget_pwd($uid,$expire,$mkey,$type=0) {
		$query = $this->db->get_where('yy_user_forgotpass', array('uid' => $uid,'type'=>$type), 1, 0);
		$this->db->order_by("expire","DESC");
		$data = array('uid'=>$uid,'expire'=>$expire,'authen'=>$mkey,'type'=>$type);
		if ($query->num_rows() > 0){
			//Update
			$info = $query->row_array();
		    $this->db->where('uid',$info['uid']);
		    $this->db->update('yy_user_forgotpass',$data);
		}else{
			//Insert
			$this->db->insert('yy_user_forgotpass',$data);
		}
		return $this->db->affected_rows()>0;
	}
    
}

/* End of file School_user_model.php */
/* Location: ./application/models/school/school_user_model.php */